<?php

class FormElement_Image extends FormElement
{

  public function render($row, $action)
  {
    $this->beforeRender($row, $action);

    $this->set('thumb_src', $this->getThumb($this->getValue($row), 100, 100));
    $this->set('image_src', $this->getThumb($this->getValue($row)));

    $this->set('value', $this->getValue($row));
    $this->set('class', $this->getClass($row, $action));

    $this->set('action', $action);
    $this->set('name', $this->name);
    $this->set('title', $this->title);
    $this->set('typeClass', $this->getTypeClass());
    $this->set('instanceClass', $this->getInstanceClass());
    $this->set('enabled', $this->enabled);

    $output = $this->getView();

    $output = $this->afterRender($output, $row, $action);

    return $output;
  }

  public function setDataFromPost(&$row, $post, $action)
  {
    $upload = $this->getUploadComponent();

    $error = null;

    try {
      $upload->upload($this->getField());

      $old = $this->getValue($row);
      if (! empty($old) && file_exists($this->getFilesDir() . $old)) {
        unlink($this->getFilesDir() . $old);
      }

      $this->setValue($row, $upload->getUploadedPath());
    }
    catch (UploadException $e) {
      if ($action == Form::ACTION_CREATE) {
        $error = $upload->getError();
      }
    }
    catch (ValidationException $e) {
      $error = $upload->getError();
    }

    if ($error) {
      throw new ValidationException($error);
    }
  }

  public function beforeDelete(&$row)
  {
    $old = $this->getValue($row);

    if (! empty($old) && file_exists($this->getFilesDir() . $old)) {
      unlink($this->getFilesDir() . $old);
    }
  }

  protected function getThumb($file, $width = null, $height = null)
  {
    try {
      if (! file_exists($this->getFilesDir() . $file)) {
        $src = false;
      }

      if ($width && $height) {
        $thumb = $this->getThumbComponent();

        $thumb->setParam('config_cache_directory', $this->getCacheDir());
        $thumb->setParam('config_document_root', $this->getFilesDir());

        $thumb->parse($file);

        $src = $this->getCacheUrl() . '/' . $thumb->getCacheFilename($width, $height);
      }
      else {
        $src = $this->getFilesUrl() . $file;
      }
    }
    catch (Exception $e) {
      $src = false;
    }

    return $src;
  }

  protected function getFilesUrl()
  {
    return s::config()->get('files_url');
  }

  protected function getCacheUrl()
  {
    return s::config()->get('files_url') . '/cache';
  }

  protected function getFilesDir()
  {
    return s::config()->get('files_dir');
  }

  protected function getCacheDir()
  {
    return s::config()->get('files_dir') . '/cache';
  }

  /**
   *
   * @return PhpThumbComponent
   */
  protected function getThumbComponent()
  {
    return Component::factory('php_thumb');
  }

  /**
   *
   * @return UploadComponent
   */
  protected function getUploadComponent()
  {
    return Component::factory('upload');
  }

}

?>